package lambda_expression.stream;

import lambda_expression.method_reference.Employee;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class FindStreamTest {

    public static void main(String[] args) {
        List<Employee> employeeList = new ArrayList<Employee>();
        employeeList.add(new Employee(1, "ABC", 100, 1120.0));
        employeeList.add(new Employee(2, "DEF", 200, 220.0));
        employeeList.add(new Employee(3, "QWE", 300, 330.0));
        employeeList.add(new Employee(4, "RTY", 400, 9999.0));
        employeeList.add(new Employee(4, "POI", 200, 10.0));
        employeeList.add(new Employee(4, "UYT", 300, 665.0));

        Optional<Employee> empOptional = employeeList.stream()
                .findFirst();

        empOptional.ifPresentOrElse(
                employee -> {
                    employee.setName("张三");
                },
                () -> {
                    System.out.println("Optional is null");
                }
        );

        System.out.println("empOptional2 = " +
                empOptional.orElse(new Employee(4, "UYT", 300, 10.0)));

        Optional<Employee> empOptional3 = employeeList.stream()
                .findAny(); // 并行上使用

        System.out.println("empOptional3 = " +
                empOptional3.orElse(new Employee(40, "FIND", 300, 10.0)));

    }
}
